﻿using ArtMath.Component;
using ArtMath.Component.Point;
using ArtMath.Core.Data;
using ArtMath.CommonType;
using ArtMath.Data;
using ArtMath.Resource;
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace ArtMath.Parameter
{
    /// <summary>
    /// 只读参数
    /// </summary>
    [Serializable]
    public class ReadOnlyParameter : BasicParameter
    {
        public ReadOnlyParameter(Document doc, string name, string value) : base(doc, name, value, double.NegativeInfinity.ToString(), "0", double.PositiveInfinity.ToString())
        {
            ParametersManagerItem.ImageKey = "readonly.png";
        }
        [Browsable(false)]
        public override DocStringX Step
        {
            get
            {
                return base.Step;
            }

            set
            {
                base.Step = value;
            }
        }
        [Browsable(false)]
        public override DocStringX InitialValue
        {
            get
            {
                return base.InitialValue;
            }

            set
            {
                base.InitialValue = value;
            }
        }
        [Browsable(false)]
        public override DocStringX TerminalValue
        {
            get
            {
                return base.TerminalValue;
            }

            set
            {
                base.TerminalValue = value;
            }
        }
        [ReadOnly(true)]
        public override DocStringX Value
        {
            get
            {
                return base.Value;
            }

            set
            {
                base.Value = value;
            }
        }
        [ReadOnly(true)]
        public override ParameterUnit Unit
        {
            get
            {
                return base.Unit;
            }

            set
            {
                base.Unit = value;
            }
        }
        public override bool CanRecover
        {
            get
            {
                if (!base.CanRecover) return false;
                bool can = true;
                ForEachRelatedObject((Base obj) =>
                {
                    if (obj.ObjectState != ObjectState.Normal)
                    {
                        can = false;
                    }
                    return can;
                });
                return can;
            }
        }
        protected virtual void ForEachRelatedObject(Predicate<Base> action)
        {

        }
        public override void Recover()
        {
            ForEachRelatedObject((Base obj) =>
            {
                if (obj.ObjectState != ObjectState.Normal)
                {
                    //尝试恢复obj
                    if (obj.CanRecover)
                    {
                        try
                        {
                            obj.Recover();
                            return true;
                        }
                        catch (Exception)
                        {

                        }
                    }
                    throw new ObjectDisposedException(obj.Name, string.Format(Resources.Translate("CannotFindObject"), obj.Name));
                }
                return true;
            });
            base.Recover();
        }
        public virtual void ChangePoint(PointBase oldPoint, PointBase newPoint)
        {

        }
    }
}
